package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 对Jboss所在OS的相关指标采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:47:39          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class OSinfoCollector extends Collector {
    private static Logger logger = Logger.getLogger(OSinfoCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        try {
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            ObjectName jbossServer = new ObjectName("jboss.system:type=Server");
            ObjectName jbossServerInfo = new ObjectName("jboss.system:type=ServerInfo");

            String HostName = (String) rmiAdapter.getAttribute(jbossServerInfo, "HostName") + "";
            String OSName = (String) rmiAdapter.getAttribute(jbossServerInfo, "OSName") + "";
            String OSArch = (String) rmiAdapter.getAttribute(jbossServerInfo, "OSArch") + "";
            String OSVersion = (String) rmiAdapter.getAttribute(jbossServerInfo, "OSVersion") + "";
            String availableProcessors = rmiAdapter.getAttribute(jbossServerInfo, "AvailableProcessors") + "";
            String JbossVersion = ((String) rmiAdapter.getAttribute(jbossServer, "Version")).replaceAll("[(].*[)]", "") + "";

            propValues[0] = ResPropertyValue.createPropValue(HostName, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(OSArch, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(OSName, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(OSVersion, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(availableProcessors, propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue(JbossVersion, propMetas[5]);

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "HostName", "OSFramework", "OSName", "OSVersion", "ProcesserNum", "JBossVersion" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }

}
